package com.lemon.secondhandplatform.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;

import com.lemon.secondhandplatform.R;
import com.lemon.secondhandplatform.mode.User;
import com.lemon.secondhandplatform.ui.util.StatusBarUtils;
import com.lemon.secondhandplatform.ui.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 注册
 */
public class RegisterActivity extends BaseActivity {

    @BindView(R.id.edt_username)
    public EditText edt_username;

    @BindView(R.id.edt_password)
    public EditText edt_password;

    @BindView(R.id.toolbar)
    public Toolbar toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        StatusBarUtils.drawableStatus(this, R.color.colorPrimary);

        bindListener();
    }

    /**
     * 注册
     * @param view
     */
    @OnClick(R.id.btn_register)
    public void register(View view) {
        String username = edt_username.getText().toString();
        String password = edt_password.getText().toString();

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                if (e == null) {
                    ToastUtils.show("注册成功");
                    finish();
                } else {
                    ToastUtils.show("注册失败");
                }
            }
        });
    }

    private void bindListener() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisterActivity.this.finish();
            }
        });
    }

}
